<?php

namespace app\company\service\user_profile\repealArchive;

use app\admin\service\UserService;
use app\common\Message;
use app\model\archives_model\UserArchiveModel;
use app\model\ConsultOfflineModel;
use app\model\ConsultOnlineModel;
use app\model\DepartmentModel;
use app\model\InterfereUserModel;
use app\model\ProxyEvaluationResultModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultModel;
use app\model\UserModel;
use think\db\exception\DbException;
use think\Service;

class RepealArchiveService extends Service
{
    use Message;

    /**
     * 废除档案库列表
     * @param $data
     * @return true
     * @throws DbException
     */
    public function recordList($data): bool
    {
        $where = [
            ['unit_id','=',$data['unit_id']],
            ['role_mode','=',3],
        ];
        /*搜索条件-开始*/
        $searchKey = ['name','department_id','start_time'];
        foreach ($searchKey as $v) {
            if (check_array_key($v, $data)){
                switch ($v){
                    case 'name':
                        $where[] = ['name|username', 'like', '%' . $data[$v] . '%'];
                        break;
                    case 'department_id':
                        $where[] = ['department_id', '=', $data[$v]];
                        break;
                    case 'start_time':
                        $where[] = ['repeal_time', 'between', [$data['start_time'],$data['end_time']]];
                        break;
                }
            }
        }
        /*搜索条件-结束*/
        $where[] = ['is_repeal','=',1];
        $field = ['id,name,username,avatar,sex,province_name,age,phone,nation,city_name,area_name,department_id,unit_id,
        user_number,archive_update_time,time,student_status,id_card,status as acc_status','repeal_time'];
        $query = UserModel::query()->where($where);
        $list = $query->with(['departmentNameMode','unitName'])->field($field)->order('repeal_time','desc')->paginate($data['pageSize'])->toArray();
        if(count($list)>0){
            $UserService = new UserService($this->app);
            $DepartmentAffiliationAll = $UserService->getDepartmentAffiliationAll($data['unit_id']);
            $department_ids = array_column($list['data'], 'department_id');
            $DepartmentAdminAll = $UserService->getDepartmentAdminAll($department_ids);
            foreach ($list['data'] as &$item) {
                //部门归属
                $item['ascription_name'] = $DepartmentAffiliationAll[$item['department_id']]['ascription_name']??'';
                if($item['archive_update_time'] == null) $item['archive_update_time'] = $item['time'];
                //所在部门
                $item[''] = $DepartmentAffiliationAll[$item['department_id']]['name']??'';
                //部门number（班级编号）
                $item['department_number'] = $DepartmentAffiliationAll[$item['department_id']]['number']??'';
                //部门管理员
                $item['department_admin'] = $DepartmentAdminAll[$item['department_id']]['user_name']??'';
            }
        }
        $list['ids'] = $query->column('id');
        $this->result = $list;
        $this->message = '操作成功！';
        return true;
    }

    /**
     * 废除档案库列表所有数据ID
     * @param $data
     * @return true
     * @throws DbException
     */
    public function recordListAll($data): bool
    {
        $where = [
            ['unit_id','=',$data['unit_id']],
            ['role_mode','=',3],
        ];
        /*搜索条件-开始*/
        $searchKey = ['name','department_id','start_time'];
        foreach ($searchKey as $v) {
            if (check_array_key($v, $data)){
                switch ($v){
                    case 'name':
                        $where[] = ['name|username', 'like', '%' . $data[$v] . '%'];
                        break;
                    case 'department_id':
                        $where[] = ['department_id', '=', $data[$v]];
                        break;
                    case 'start_time':
                        $where[] = ['repeal_time', 'between', [$data['start_time'],$data['end_time']]];
                        break;
                }
            }
        }
        /*搜索条件-结束*/
        $where[] = ['is_repeal','=',1];
        $field = ['id,name,username,avatar,sex,province_name,age,phone,nation,city_name,area_name,department_id,unit_id,
        user_number,archive_update_time,time,student_status,id_card,status as acc_status'];
        $list = UserModel::query()->where($where)->with(['departmentNameMode','unitName'])->field($field)->select();
        $list = $list ? $list->toArray() : ['data' => []];
        $this->result = $list;
        $this->message = '操作成功！';
        return true;
    }

}